package com.ican.service.product;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ican.common.SystemConstants;
import com.ican.common.hibernate.Page;
import com.ican.dao.product.LanguageDao;
import com.ican.dao.product.ProductAttributeDao;
import com.ican.dao.product.CategoryDao;
import com.ican.dao.product.ProductDao;
import com.ican.dao.product.ProductImageDao;
import com.ican.domain.product.Language;
import com.ican.domain.product.Product;
import com.ican.domain.product.ProductAttribute;
import com.ican.domain.product.Category;
import com.ican.domain.product.ProductImage;
import com.ican.service.product.ProductService;

@Service("productService")
public class ProductService{

    private LanguageDao languageDao;
    private CategoryDao categoryDao;
    private ProductDao productDao;
    private ProductAttributeDao productAttributeDao;
    private ProductImageDao productImageDao;

    //语言相关
    public Language findLanguage(Integer id){
        return languageDao.get(id);
    }
    public Language findLanguageByCode(String code){
        return languageDao.findLanguageByCode(code);
    }
    public List<Language> findLanguage(){
        return languageDao.findLanguage();
    }
    
    //类目相关
    public void addCategory(Category category){
        Category entity = categoryDao.findCategoryByNameAndCodeAndLang(category.getName(), category.getCode(), category.getLangId());
        if(entity == null){
            category.setCreateTime(new Date());
            category.setModifyTime(new Date());
            category.setDataStatus(SystemConstants.ENTITY_DATE_STATUS_YES);
            category.setSynStatus(SystemConstants.ENTITY_DATE_SYN_STATUS_NO);
            category.setNcStatus(SystemConstants.NC_STATUS_NO);
            category.setNcNum(0);
            category.setNcNumFail(0);
            category.setNcNumNo(0);
            categoryDao.save(category);
        }else{
            category.setId(entity.getId());
            if(category.getName() != null){
                entity.setName(category.getName());
            }
            if(category.getUrl() != null){
                entity.setUrl(category.getUrl());
            }
            if(category.getDescription() != null){
                entity.setDescription(category.getDescription());
            }
            if(category.getLangId() != null){
                entity.setLangId(category.getLangId());
            }
            if(category.getRuleId() != null){
                entity.setRuleId(category.getRuleId());
            }
            entity.setModifyTime(new Date());
            categoryDao.save(entity);
        }
    }
    public Category findCategory(Integer id){
        return categoryDao.get(id);
    }
    public List<Category> findCategoryByName(String name){
        return categoryDao.findCategoryByName(name);
    }
    public List<Category> findCategoryByCode(String code){
        return categoryDao.findCategoryByCode(code);
    }
    public List<Category> findCategoryByNameAndCode(String name, String code){
        return categoryDao.findCategoryByNameAndCode(name, code);
    }
    public Category findCategoryByNameAndCodeAndLang(String name, String code, Integer langId){
        return categoryDao.findCategoryByNameAndCodeAndLang(name, code, langId);
    }
    public Page<Category> findAllCategory(Page<Category> page, Category category){
        return categoryDao.findAllCategory(page, category);
    }
    public List<Integer> findAllCategoryIdsToSynAuto(){
        return categoryDao.findAllCategoryIdsToSynAuto();
    }
    public void updateCategorySynStatus(Integer cateId, Integer synStatus){
        categoryDao.updateCategorySynStatus(cateId, synStatus);
    }
    public void updateCategorySynCateId(String code, Integer synCategoryId){
        categoryDao.updateCategorySynCateId(code, synCategoryId);
    }
    public void updateCategoryNcStatus(Integer cateId, Integer ncStatus){
        categoryDao.updateCategoryNcStatus(cateId, ncStatus);
    }
    public void updateCategoryNcNum(){
        categoryDao.updateCategoryNcNum();
    }
    public List<Integer> findAllCategoryIdsToNcAuto(){
        return categoryDao.findAllCategoryIdsToNcAuto();
    }
    
    //商品相关
    public void addProduct(Product product){
        Product entity = productDao.findProductByUrlFlagAndLang(product.getUrlFlag(), product.getLangId());
        if(entity == null){
            product.setCreateTime(new Date());
            product.setModifyTime(new Date());
            product.setDataStatus(SystemConstants.ENTITY_DATE_STATUS_YES);
            product.setSynStatus(SystemConstants.ENTITY_DATE_SYN_STATUS_NO);
            product.setNcStatus(SystemConstants.NC_STATUS_NO);
            productDao.save(product);
        }else{
            product.setId(entity.getId());
            if(product.getDescription() != null){
                entity.setDescription(product.getDescription());
            }
            if(product.getPrice() != null){
                entity.setPrice(product.getPrice());
            }
            if(product.getPriceUnit() != null){
                entity.setPriceUnit(product.getPriceUnit());
            }
            if(product.getWeight() != null){
                entity.setWeight(product.getWeight());
            }
            if(product.getWeightUnit() != null){
                entity.setWeightUnit(product.getWeightUnit());
            }
            entity.setModifyTime(new Date());
            productDao.save(entity);
        }
    }
    public Product findProduct(Integer id){
        return productDao.get(id);
    }
    public List<Product> findProductByName(String name){
        return productDao.findProductByName(name);
    }
    public List<Product> findProductByCode(String code){
        return productDao.findProductByCode(code);
    }
    public List<Product> findProductByNameAndCode(String name, String code){
        return productDao.findProductByNameAndCode(name, code);
    }
    public Product findProductByNameAndCodeAndLang(String name, String code, Integer langId){
        return productDao.findProductByNameAndCodeAndLang(name, code, langId);
    }
    public Page<Product> findAllProduct(Page<Product> page, Product product){
        return productDao.findAllProduct(page, product);
    }
    public Product findAllProductToSynById(Integer id){
        return productDao.findAllProductToSynById(id);
    }
    public List<Integer> findAllProductIdsToSynByCateId(Integer cateId){
        return productDao.findAllProductIdsToSynByCateId(cateId);
    }
    public void updateProductSynStatus(Integer proId, Integer synStatus){
        productDao.updateProductSynStatus(proId, synStatus);
    }
    public void updateProductSynProId(String code, Integer synProductId){
        productDao.updateProductSynProId(code, synProductId);
    }
    public void updateProductNcStatus(Integer proId, Integer ncStatus){
        productDao.updateProductNcStatus(proId, ncStatus);
    }
    public void updateProduct(Product product){
        productDao.updateProduct(product);
    }
    public List<Integer> getAllProductIdForNcByCategory(Integer categoryId, Integer ncStatus){
        return productDao.getAllProductIdForNcByCategory(categoryId, ncStatus);
    }
    
    
    //属性相关
    public void addProductAttr(ProductAttribute productAttribute){
        productAttribute.setCreateTime(new Date());
        productAttribute.setModifyTime(new Date());
        productAttributeDao.save(productAttribute);
    }
    public List<ProductAttribute> findProductAttr(Integer productId, String attr, String value, Integer langId){
        return productAttributeDao.findProductAttr(productId, attr, value, langId);
    }
    public List<ProductAttribute> findProductAttrByProductId(Integer productId){
        return productAttributeDao.findProductAttrByProductId(productId);
    }
    public void updateAttrOptionName(String nameNew, String name, Integer langId){
        productAttributeDao.updateAttrOptionName(nameNew, name, langId);
    }
    public void updateAttrOptionCode(String name, Integer langId, String nameCode){
        productAttributeDao.updateAttrOptionCode(name, langId, nameCode);
    }
    public void updateAttrOptionCode(Integer id, String nameCode){
        productAttributeDao.updateAttrOptionCode(id, nameCode);
    }
    public void updateAttrOptionValueName(String nameCode, String optionValue, Integer langId, String optionValueNew){
        productAttributeDao.updateAttrOptionValueName(nameCode, optionValue, langId, optionValueNew);
    }
    public void updateAttrOptionValueCode(String nameCode, String optionValue, String valueCode, Integer langId){
        productAttributeDao.updateAttrOptionValueCode(nameCode, optionValue, valueCode, langId);
    }
    public void updateAttrOptionValueCode(Integer id, String valueCode){
        productAttributeDao.updateAttrOptionValueCode(id, valueCode);
    }
    public void deleteductAttr(String name, Integer langId){
        productAttributeDao.deleteductAttr(name, langId);
    }
    public void deleteductAttr(String nameCode, String optionValue, Integer langId){
        productAttributeDao.deleteductAttr(nameCode, optionValue, langId);
    }
    
    //图片相关
    public void deleteProductImage(Integer productId){
        productImageDao.deleteProductImageByProductId(productId);
    }
    public void addProductImage(ProductImage productImage){
        productImage.setCreateTime(new Date());
        productImage.setModifyTime(new Date());
        productImageDao.save(productImage);
    }
    public List<ProductImage> findProductImageByProductIdAndCode(Integer productId, String code){
        return productImageDao.findProductImageByProductIdAndCode(productId, code);
    }
    public List<ProductImage> findProductImageByProductId(Integer productId){
        return productImageDao.findProductImageByProductId(productId);
    }
    
    @Resource
    public void setCategoryDao(CategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }
    @Resource
    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }
    @Resource
    public void setProductAttributeDao(ProductAttributeDao productAttributeDao) {
        this.productAttributeDao = productAttributeDao;
    }
    @Resource
    public void setProductImageDao(ProductImageDao productImageDao) {
        this.productImageDao = productImageDao;
    }
    @Resource
    public void setLanguageDao(LanguageDao languageDao) {
        this.languageDao = languageDao;
    }
}
